﻿@model List<NCP.Model.Role>
@{
    ViewBag.Title = "添加角色";
}
<div class="layui-fluid">
    <input type="hidden" id="UserCode" name="UserCode" value="@ViewBag.UserCode" />
    <div class="layui-card layui-form" lay-filter="layuiadmin-form-list" id="layuiadmin-form-list" style="padding: 20px 30px 0 0;">
        <div class="layui-card-body">
            <div class="layui-form-item">
                <label for="name" class="layui-form-label">
                    <span class="x-red">*</span>用户名
                </label>
                <div class="layui-input-inline">
                    <input type="text" readonly="readonly" value="@ViewBag.UserName"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">
                    拥有角色
                </label>
                <table class="layui-table layui-input-block">
                    <tbody>
                        <tr>
                            <td>
                                <div class="layui-input-block">
                                    @foreach (var item in Model)
                                    {
                                        if (item.IsChecked == "1")
                                        {
                                            <input name="@item.RoleCode" lay-skin="primary" checked type="checkbox" title="@item.RoleName" value="@item.Id">
                                        }
                                        else
                                        {
                                            <input name="@item.RoleCode" lay-skin="primary" type="checkbox" title="@item.RoleName" value="@item.Id">
                                        }
                                    }
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit lay-filter="layuiadmin-form-submit" id="layuiadmin-form-submit">立即提交</button>
                <button class="layui-btn layui-btn-danger" type="button" onclick="onClose()">
                    关闭
                </button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
        var $ = layui.$
            , form = layui.form
            , admin = layui.admin;
        form.render(null, 'layuiadmin-form-list');
        //监听提交
        form.on('submit(layuiadmin-form-submit)', function (data) {
            var field = data.field; //获取提交的字段
            //请求登入接口
            admin.req({
                url: '/Role/GrantRole' //实际使用请改成服务端真实接口
                , method: 'post'
                , data: "UserCode=" + $("#UserCode").val() + "&Role=" + JSON.stringify(field)
                , done: function (res) {
                    layer.msg(res.msg, {
                        offset: '50px'
                        , icon: 1
                        , time: 3000
                    }, function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layui.table.reload('LAY-user-list'); //重载表格
                        parent.layer.close(index); //再执行关闭
                    });
                }
            });
        });
    })

    function onClose() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
</script>

